/*
* ClassroomServiceImpl.java
* Created on  2014-8-5 下午4:48
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-8-5       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.classroom.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classroom.dao.ClassroomDao;
import com.genghis.ptas.classroom.entity.ClassroomCourse;
import com.genghis.ptas.classroom.entity.ClassroomInfo;
import com.genghis.ptas.classroom.entity.ClassroomStudent;
import com.genghis.ptas.classroom.service.ClassroomService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.inbox.dao.InboxDao;
import com.genghis.ptas.inbox.entity.Inbox;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.task.service.ExaminationService;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Transactional
@Service("classroomService")
public class ClassroomServiceImpl implements ClassroomService {
    @Autowired
    private ClassroomDao classroomDao;

    @Autowired
    private UserService userService;

    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private DictService dictService;

    @Autowired
    private InboxDao inboxDao;

    @Override
    public Page<ClassroomInfo> findClassroomInfoPageList(PageParameter pageParameter, ClassroomInfo classroomInfo) {
        Page<ClassroomInfo> classroomInfoPage = classroomDao.findClassroomInfoPageList(pageParameter, classroomInfo);
        return classroomInfoPage;
    }

    @Override
    public void deleteClassroomInfoById(Integer id) {
        classroomDao.deleteClassroomInfoById(id);
    }

    @Override
    public void deleteClassroomCourseByClassroomId(Integer id) {
        classroomDao.deleteClassroomCourseByClassroomId(id);
    }

    @Override
    public int addClassroomInfo(ClassroomInfo classroomInfo) {
        String[] courseList = classroomInfo.getClassroomCourseList().split(";");
        int classroomInfoId = classroomDao.addClassroomInfo(classroomInfo);
        ClassroomCourse classroomCourse = new ClassroomCourse();
        for (int i = 0; i < courseList.length; i++) {
            String[] courseInfo = courseList[i].split(",");
            classroomCourse.setClassroomId(classroomInfoId);
            classroomCourse.setCourseId(Integer.parseInt(courseInfo[0]));
            classroomCourse.setCourseNumber(Integer.parseInt(courseInfo[1]));
            classroomDao.addClassroomCourse(classroomCourse);
        }
        return classroomInfoId;
    }

    @Override
    public ClassroomInfo findClassroomInfoById(int taskId) {
        ClassroomInfo classroomInfo = classroomDao.findClassroomInfoById(taskId);
        classroomInfo.setExaminationInfo(examinationService.findExaminationById(classroomInfo.getExaminationId()));
        return classroomInfo;
    }

    @Override
    public void editClassroomInfo(ClassroomInfo classroomInfo) {
        classroomDao.editClassroomInfo(classroomInfo);
    }

    @Override
    public List<ClassroomInfo> findAllClassroomInfoList() {
        List<ClassroomInfo> classroomInfoList = classroomDao.findAllClassroomInfoList();
        if (LoginUserUtil.findLoginUserName() != null) {
            int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
            for (ClassroomInfo classroomInfo : classroomInfoList) {
                List<ClassroomStudent> classroomStudent = classroomDao.findClassroomStudentByStudentIdAndClassroomInfoId(userId, classroomInfo.getId());
                if (classroomStudent.size() != 0) {
                    classroomInfo.setClassroomStudentInfo(classroomStudent.get(0));
                }
            }
        }
        return classroomInfoList;
    }

    @Override
    public List<ClassroomCourse> findClassroomCourseByClassroomInfoId(Integer classroomInfoId) {
        return classroomDao.findClassroomCourseByClassroomInfoId(classroomInfoId);
    }

    @Override
    public void addClassroomStudent(Integer classroomId) {
        Integer userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        ClassroomStudent classroomStudent = new ClassroomStudent(userId, 0, classroomId);
        classroomDao.addClassroomStudent(classroomStudent);
    }

    @Override
    public List<ClassroomCourse> findClassroomCourseByClassroomInfoIdAndUserId(Integer id, int userId) {
        List<ClassroomCourse> classroomCourseList = classroomDao.findClassroomCourseByClassroomInfoId(id);
        List<ClassroomStudent> classroomStudent = classroomDao.findClassroomStudentByStudentIdAndClassroomInfoId(userId, id);
        if (classroomStudent.size() != 0) {
            for (ClassroomCourse classroomCourse : classroomCourseList) {
                //判断课程状态
                if (classroomStudent.get(0).getCourseNumber() == null) {
                    classroomCourse.setStudentState(DictConstant.NOTATTEMPTED_TRANSLATION);
                } else if (classroomStudent.get(0).getCourseNumber() < classroomCourse.getCourseNumber()) {
                    classroomCourse.setStudentState(DictConstant.NOTATTEMPTED_TRANSLATION);
                } else {
                    classroomCourse.setStudentState(DictConstant.COMPLETE_TRANSLATION);
                }
            }
        }
        return classroomCourseList;
    }

    @Override
    public void changeStudentCourseId(ClassroomStudent classroomStudent) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        classroomStudent.setStudentId(userId);
        List<ClassroomCourse> classroomCourseList = findClassroomCourseByClassroomInfoId(classroomStudent.getClassroomInfoId());
        List<ClassroomStudent> userStudentCourse = classroomDao.findClassroomStudentByStudentIdAndClassroomInfoId(classroomStudent.getClassroomInfoId(), userId);
        int nowStudentNumber = 0, lastStudentNumber = 0;
        if (userStudentCourse.size() != 0) {
            // 判断课件排位数大小
            for (ClassroomCourse classroomCourse : classroomCourseList) {
                if (classroomCourse.getCourseId() == classroomStudent.getClassroomCourseId())
                    nowStudentNumber = classroomCourse.getCourseNumber();
                if (classroomCourse.getCourseId() == userStudentCourse.get(0).getClassroomCourseId())
                    lastStudentNumber = classroomCourse.getCourseNumber();
            }
            if (nowStudentNumber < lastStudentNumber)
                return;
            else
                classroomDao.changeStudentCourseId(classroomStudent);
        } else
            classroomDao.changeStudentCourseId(classroomStudent);
    }

    @Override
    public List<ClassroomInfo> findExpendClassroom(String classroomCourseName) {
        List<ClassroomInfo> classroomInfoList = classroomDao.findExpendClassroom(classroomCourseName.substring(0, Math.min(3, classroomCourseName.length())));
        while (classroomInfoList.size() < 4) {
            classroomInfoList.addAll(classroomDao.findAllClassroomInfoList());
        }
        return classroomInfoList;
    }

    @Override
    public boolean judgeReapedClassroomStudent(Integer classroomId) {
        Integer userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        List<ClassroomStudent> studentList = classroomDao.findClassroomStudentByStudentIdAndClassroomInfoId(userId, classroomId);
        if (studentList.size() == 0)
            return true;
        else
            return false;
    }

    public void doTask() {
        List<ClassroomInfo> classroomInfoList = classroomDao.findAllClassroomInfoList();
        for (ClassroomInfo classroomInfo : classroomInfoList) {
            List<ClassroomStudent> classroomStudents = classroomDao.findClassroomStudentByClassroomInfoId(classroomInfo.getId());
            int CourseNumber = classroomDao.findMaxCourseNumberByCourseId(classroomInfo.getId());
            for (ClassroomStudent classroomStudent : classroomStudents) {
                if (classroomStudent.getCourseNumber() < CourseNumber) {
                    setEmail(classroomStudent);
                }
            }
        }
    }

    private void setEmail(ClassroomStudent classroomStudent) {
        Inbox inbox = new Inbox();
        inbox.setAddresseeId(0);
        inbox.setUserId(classroomStudent.getStudentId());
        inbox.setSendDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        inbox.setCategory(DictConstant.INBOX_CATEGORY_1);
        inbox.setState(DictConstant.INBOX_STATE_0);
        int courseNumber = classroomStudent.getCourseNumber() + 1;
        inbox.setContent("系统提示：您当前该学习第" + courseNumber + "节课程了，抓紧学习吧！");
        inboxDao.addInbox(inbox);
    }
}
